package com.zlsy.thread.sixstates;

/**
 * @author zhouliang
 * @date 2020/2/27 18:55
 * @Desc 展示线程的后面三种状态：TIMED_WAITING 、 BLOCKED 、 WAITING
 */
public class BlockedWaitingTimedWaiting implements Runnable {

    public static void main(String[] args) throws InterruptedException {
        BlockedWaitingTimedWaiting runable = new BlockedWaitingTimedWaiting();
        Thread thread1 = new Thread(runable);
        thread1.setName("thread1");
        Thread thread2 = new Thread(runable);
        thread2.setName("thread2");
        thread1.start();
        thread2.start();
        //等到50毫秒好让线程已经获取到cpu资源直接进入方法执行sleep
        Thread.sleep(50);
        System.out.println(thread1.getState());
        System.out.println(thread2.getState());
        //等待thread1睡眠的时间，此时按照推理，thread1已经进入了wait();
        Thread.sleep(1300);
        //那么thread1的线程状态应该是
        System.out.println(thread1.getState());
    }

    @Override
    public void run() {
        syn();
    }

    private synchronized void syn() {
        try {
            Thread.sleep(1000);
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName());
    }
}
